package yl.hs.bmipfull.service.laboratorydata.imple;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmFlyashInspectionItemDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmFlyashReportItemDao;
import yl.hs.bmipfull.daos.laboratorydata.ILabRmInspectionReportDao;
import yl.hs.bmipfull.pojo.laboratorydata.LabExperimentDefinitionlibsEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmFlyashInspectionItemEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmFlyashReportItemEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabRmInspectionReportEntity;
import yl.hs.bmipfull.service.laboratorydata.ILabRmFlyashReportItemService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * @author 吴高丰
 * @apiNote 检测报告-粉煤灰报告服务层接口实现
 * @since 2022年06月30日 09时38分31秒 <br/>
 */
@Service
public class LabRmFlyashReportItemServiceImpl extends LaboratorydataBaseService implements ILabRmFlyashReportItemService {

    @Autowired
    ILabRmFlyashReportItemDao dao;
    @Autowired
    ILabRmFlyashInspectionItemDao itemdao;
    @Autowired
    ILabRmInspectionReportDao reportDao;
    @Override
    public List<LabRmFlyashReportItemEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabRmFlyashReportItemEntity.class);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabRmFlyashReportItemEntity.class);
        return dao.queryList(pageQuery.getQuery(), filters);
    }

    @Override
    public PageResult<LabRmFlyashReportItemEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabRmFlyashReportItemEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabRmFlyashReportItemEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),LabRmFlyashReportItemEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public LabRmFlyashReportItemEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveLabRmFlyashReportItem(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ObjectMapper objectMapper = new ObjectMapper();
        if (Tl.isEmpty(sn)) {
            LabRmInspectionReportEntity reportEntity = new LabRmInspectionReportEntity();
            Tl.getChangeLog(reportEntity,model);
            reportEntity.setPYFieldValue();
            reportEntity.setSn(queryNewSN(LabRmInspectionReportEntity.class));
            result =  reportDao.insertNew(reportEntity);
            if (result > 0) {
                writeLog(ActionType.CREATE, reportEntity.getSn(), "插入信息", Tl.toJson(reportEntity), LabRmInspectionReportEntity.class);
            }
            result=0;
            var subData=model.get("subData");
            var subDataEntity=objectMapper.convertValue(subData, LabRmFlyashReportItemEntity.class);
            subDataEntity.setReportSN(reportEntity.getSn());
            result = dao.insertNew(subDataEntity);
            if (result > 0) {

                writeLog(ActionType.CREATE, reportEntity.getSn(), "插入检测报告-粉煤灰报告信息", Tl.toJson(subDataEntity), LabRmFlyashReportItemEntity.class);
            }

        } else {
            var reportentity = reportDao.findBySN(sn);
            Tl.getChangeLog(reportentity,model);
            reportentity.setPYFieldValue();
            var resut1 = reportDao.update(reportentity);
            dao.deleteByReportSN(reportentity.getSn());
            var subData=model.get("subData");
            var subDataEntity=objectMapper.convertValue(subData, LabRmFlyashReportItemEntity.class);
            subDataEntity.setReportSN(reportentity.getSn());
            result = dao.insertNew(subDataEntity);
            if (result > 0) {

                writeLog(ActionType.CREATE, reportentity.getSn(), "插入检测报告-粉煤灰报告信息", Tl.toJson(subDataEntity), LabRmFlyashReportItemEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String sn) {
        Integer result = dao.deleteByReportSN(sn);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, LabRmFlyashReportItemEntity.class);
        }
        return result;
    }

    @Override
    public List<LabRmFlyashReportItemEntity> findByReportSN(String reportSN) {
        return dao.findByReportSN(reportSN);
    }
}